package model

import "errors"

type User struct {
	ID    string `json:"id"`
	Name  string `json:"name"`
	Email string `json:"email"`
}

// 模拟数据库
var users = make(map[string]User)

func InitDB() {
	// 初始化一些测试数据
	users["1"] = User{ID: "1", Name: "Alice", Email: "alice@example.com"}
	users["2"] = User{ID: "2", Name: "Bob", Email: "bob@example.com"}
}

func GetAllUsers() ([]User, error) {
	var result []User
	for _, user := range users {
		result = append(result, user)
	}
	return result, nil
}

func GetUser(id string) (*User, error) {
	if user, ok := users[id]; ok {
		return &user, nil
	}
	return nil, errors.New("user not found")
}

func CreateUser(user *User) error {
	if _, exists := users[user.ID]; exists {
		return errors.New("user already exists")
	}
	users[user.ID] = *user
	return nil
}

func UpdateUser(id string, user *User) error {
	if _, exists := users[id]; !exists {
		return errors.New("user not found")
	}
	users[id] = *user
	return nil
}

func DeleteUser(id string) error {
	if _, exists := users[id]; !exists {
		return errors.New("user not found")
	}
	delete(users, id)
	return nil
}
